package com.csweimei.rentmanagementtreasure.pop;

import android.content.Context;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.adapter.QuarterAdapter;
import com.csweimei.rentmanagementtreasure.http.entity.QuarterEntity;
import com.csweimei.rentmanagementtreasure.utils.ValidateUtils;
import com.lxj.xpopup.core.BottomPopupView;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.utils.ToastUtils;

public class PopQuarters extends BottomPopupView {

    private PopSelClick mPopSelClick;
    private TextView cancel,sure,add_quarters;
    private Context mContext;
    private  List<QuarterEntity.DataDTO> mData = new ArrayList<>();

    private QuarterAdapter adapter;
    RecyclerView recyclerView;

    private String sel;

    private String adds;

    public PopQuarters(@NonNull Context context, List<QuarterEntity.DataDTO> data, PopSelClick popSelClick) {
        super(context);
        mContext = context;
        mPopSelClick = popSelClick;
        mData.clear();
        mData.addAll(data);
    }


    @Override
    protected int getImplLayoutId() {
        return R.layout.pop_quarters;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        cancel = findViewById(R.id.cancel);
        sure = findViewById(R.id.sure);
        add_quarters = findViewById(R.id.add_quarters);
        recyclerView = findViewById(R.id.recycler);

        adapter = new QuarterAdapter();
        adapter.addChildClickViewIds(R.id.name);
        adapter.setOnItemChildClickListener((adapter1, view, position) -> {
            adapter.setSel(position);
            sel = mData.get(position).getQuarters();
            adds = mData.get(position).getAddress();
        });
        recyclerView.setAdapter(adapter);
        adapter.setList(mData);

        sure.setOnClickListener(v -> {
            if(ValidateUtils.isEmptyString(sel)){
                ToastUtils.showShort("请选择小区");
                return;
            }
            mPopSelClick.sel(new PopBean(sel,adds));
            dismiss();
        });

        cancel.setOnClickListener(v -> dismiss());


        add_quarters.setOnClickListener(v -> {
            dismiss();
            PopAddQuarters popAddQuarters = new PopAddQuarters(mContext, new PopSelClick() {
                @Override
                public void sel(PopBean x) {
                    mPopSelClick.sel(x);
                }
            });

            PopUtils.showPop(mContext,popAddQuarters,true);
        });
    }
}
